import React, { Component } from 'react'
import dayjs from 'dayjs'
import classNames from 'classnames'

export default class List extends Component {
  // 处理时间
  formatTime = (time, str = 'YYYY-MM-DD HH:mm:ss') => {
    return dayjs(time).format(str)
  }
  render() {
    return (
      <div className="comment-list">
        {this.props.list.map((item) => (
          <div className="list-item" key={item.id}>
            <div className="user-face">
              <img className="user-head" src={item.avatar} alt="" />
            </div>
            <div className="comment">
              <div className="user">{item.author}</div>
              <p className="text">{item.comment}</p>
              <div className="info">
                <span className="time">{this.formatTime(item.time)}</span>
                {/* "like liked" */}
                <span
                  className={classNames('like', { liked: item.attitude === 1 })}
                  onClick={() =>
                    this.props.changeAttitude(
                      item.id,
                      item.attitude === 1 ? 0 : 1
                    )
                  }
                >
                  <i className="icon" />
                </span>
                {/* "hate hated" */}
                <span
                  className={classNames('hate', {
                    hated: item.attitude === -1,
                  })}
                  onClick={() =>
                    this.props.changeAttitude(
                      item.id,
                      item.attitude === -1 ? 0 : -1
                    )
                  }
                >
                  <i className="icon" />
                </span>
                <span
                  className="reply btn-hover"
                  onClick={() => this.props.del(item.id)}
                >
                  删除
                </span>
              </div>
            </div>
          </div>
        ))}
      </div>
    )
  }
}
